package asher.easycat.connecter;

import java.io.IOException;
import java.net.InetAddress;
import java.net.ServerSocket;
import java.net.Socket;

import asher.easycat.processor.Processor;

/**
 * 连接器，多线程
 * 线程启动后自动监听 127.0.0.1:8088端口
 * 接受request带来的socket请求，并将该socket赋予处理器Processor
 * @author AsherYuan
 *
 */
public class Connector implements Runnable {

	boolean stopped;
	private String scheme = "http";
	
	public String getScheme() {
		return this.scheme;
	}
	
	public void run() {
		ServerSocket serverSocket = null;
		int port = 8088;
		try {
			serverSocket = new ServerSocket(port, 1, InetAddress.getByName("127.0.0.1"));
		} catch(IOException e) {
			e.printStackTrace();
			System.exit(1);
		}
		
		while(!stopped) {
			Socket socket = null;
			try {
				socket = serverSocket.accept();
			} catch (IOException e) {
				e.printStackTrace();
				continue;
			}
			
			Processor processor = new Processor(this);
			processor.process(socket);
		} 
	}
	
	public void start() {
		Thread thread = new Thread(this);
		thread.run();
	}

}
